﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Publishing;

using CSP.Core;

namespace CSP.Receivers
{
    public class MyCityFeatureReceiver : SPFeatureReceiver
    {

        public void ExcludeFromCurrentNav(string pageID, SPWeb parentWeb)
        {
            parentWeb.AllProperties["__CurrentNavigationExcludes"] = SubstituteNull(parentWeb.AllProperties["__CurrentNavigationExcludes"]) + pageID + ";";
            parentWeb.Update();
        }

        private string SubstituteNull(object o)
        {

            if (o == null)

                return String.Empty;

            else

                return o.ToString();

        }


        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            
            try
            {
                // feature is scoped at Web, so the parent is type SPWeb rather than SPSite..
                using (SPWeb web = properties.Feature.Parent as SPWeb)
                {
                    PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);

                    // MyCity area should not be anonymous. Only authenticated citizens can see and navigate it.
                    web.BreakRoleInheritance(true);
                    web.AnonymousState = SPWeb.WebAnonymousState.Disabled;
                    web.Update();

                    foreach (SPListItem li in pweb.PagesList.Items)
                    {
                        if (li.Name == "update_account.aspx")
                        {
                            ExcludeFromCurrentNav(li.UniqueId.ToString(), web);
                        }
                    }


                    ResourceFile resFile = new ResourceFile(web.Locale.Name);

                    // CitizenImages picture gallery has custom security rules. Citizens can upload their pictures.
                    SPWeb topWeb = web.Site.RootWeb;
                    SPGroup group = topWeb.Groups[resFile.LoadResource("CSP_citizensgroup_title")];
                    SPRoleAssignment ra = new SPRoleAssignment(group);

                    SPList list = web.Lists[resFile.LoadResource("CSP_citizenimages_displayname")];
                    list.BreakRoleInheritance(true);
                    list.Update();

                    SPRoleDefinitionCollection roleDefinitions = topWeb.RoleDefinitions;
                    SPRoleAssignmentCollection roleAssignments = list.RoleAssignments;

                    SPRoleAssignment roleAssignmentCitizensGroup = new SPRoleAssignment(group);

                    SPRoleDefinitionBindingCollection roleDefBindingsCitizensGroup = roleAssignmentCitizensGroup.RoleDefinitionBindings;

                    roleDefBindingsCitizensGroup.Add(roleDefinitions[resFile.LoadResource("CSP_citizenspermission_title")]);
                    roleAssignments.Add(roleAssignmentCitizensGroup);


                }
            }
            catch (Exception ex)
            {
                CSP.LoggingEngine.Utils.LogError(ex, this.GetType().Name);
            }
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }

    }
}
